<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
if($_RQ['op']=='main'){
		$basic=$this->plugin_setting('setting');
		$id = intval($_RQ['id']);
		if (empty($id)) {
				exi('参数错误','error','/index/');
		}
		$post = getPost($id);
		if (empty($post)) {
					exi('未找到话题','error','/index/');	
		}
		//$post['avatar'] = tomedia($post['avatar']);
		//$post['avatar'] = getAvatar($post['avatar']);
		$m = getMember($_SESSION['uid']);
		$board =getBoard($post['bid']);

		if (empty($board)) {
				exi('未找到版块','error','/index/');	
		}


		$isManager = isManager($board['id']);
		$pisManager = isManager($board['id'],$post['mid']);
		$isSuperManager = isSuperManager();		
		if (!($isSuperManager) && !($isManager)) {
			$check = check($m, $board);
		}

		$post['createtime']=date('Y-m-d H:i:s',$post['createtime']);
		//$post['content'] = m('ui')->lazy($post['content']);
		$post['content'] = replaceContent($post['content']);
		$post['content'] = htmlspecialchars_decode($post['content']);
		$post['images']=$images = json_decode($post['images'],true);
		pdo_update('jy_sns_post', array('views' => $post['views'] + 1), array('id' => $post['id']));
		$goodcount = pdo_count('jy_sns_like',array('pid'=>$post['id']));
		$replycount =  pdo_count('jy_sns_post',array('pid'=>$post['id'],'deleted'=>0,'checked'=>1));
		$isgood = pdo_count('jy_sns_like',array('pid'=>$post['id'],'mid'=>$_SESSION['uid']));
		$set = $this->plugin_setting('setting');;
		$member = getMember($post['mid']);
		$level = array('levelname' => (empty($set['levelname']) ? '社区粉丝' : $set['levelname']), 'color' => (empty($set['levelcolor']) ? '#333' : $set['levelcolor']), 'bg' => (empty($set['levelbg']) ? '#eee' : $set['levelbg']));

		if (!(empty($member['sns_level']))) {
			$level = pdo_get('jy_sns_level',array('id'=>$member['sns_level']));
		}
		$catelist = pdo_getall('jy_sns_complaincate',array('uniacid'=>$_SESSION['uniacid']));
		$shareImg = tomedia($board['share_icon']);

		if (!(empty($images))) {
			$shareImg = tomedia($images[0]);
		}
		$canpost = true;
		if (!($isManager) && !($isSuperManager)) {
			$check = check($m, $board, true);
			$canpost = !(is_error($check));
		}
		
		$condition = ' and `uniacid` = :uniacid and bid=:bid and pid=:pid and `deleted`=0';
		$params = array(':uniacid' => $_SESSION['uniacid'], ':pid' => $id, ':bid' => $post['bid']);
		
		if (!($isManager) && !($isSuperManager)) {
			$condition .= ' and `checked`=1';
		}


		$sql = 'select id,bid,rpid,title,createtime,content,images ,mid, nickname,avatar,checked from ' . tablename('jy_sns_post') . '  where 1 ' . $condition . ' ORDER BY createtime desc ';
		$list = pdo_fetchall($sql, $params);
		foreach ($list as $key => &$row ) {
			//$row['avatar'] = tomedia($row['avatar']);
			//$row['avatar'] =getAvatar($row['avatar']);
			$row['createtime'] = date('Y-m-d H:i', $row['createtime']);
			$row['goodcount'] = pdo_count('jy_sns_like',array('pid'=>$row['id']));
			$row['postcount'] =  pdo_count('jy_sns_post',array('pid'=>$row['id']));		
			$l=pdo_get('jy_sns_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$row['id']));
			if($l){
				$row['isgood']=1;	
			}
			$row['images'] = json_decode($row['images'],true);
			$row['imagewidth'] = '32%';
			
			$row['content'] = replaceContent($row['content']);
			$row['parent'] = false;
			if (!(empty($row['rpid']))) {
				$parentPost = getPost($row['rpid']);
				$row['parent'] = array('nickname' => $parentPost['nickname'], 'content' => replaceContent($parentPost['content']));
			}
			$isgood = pdo_fetchcolumn('select count(*) from ' . tablename('jy_sns_like') . ' where uniacid=:uniacid and pid=:pid and mid=:mid limit 1', array(':uniacid' => $_SESSION['uniacid'], ':pid' => $row['id'], ':mid' => $_SESSION['uid']));
			$row['isgood'] = $isgood;
			$row['goodcount'] = pdo_fetchcolumn('select count(*) from ' . tablename('jy_sns_like') . ' where uniacid=:uniacid and pid=:pid  limit 1', array(':uniacid' => $_SESSION['uniacid'], ':pid' => $row['id']));
			$member = getMember($row['openid']);
			$level = array('levelname' => (empty($set['levelname']) ? '社区粉丝' : $set['levelname']), 'color' => (empty($set['levelcolor']) ? '#333' : $set['levelcolor']), 'bg' => (empty($set['levelbg']) ? '#eee' : $set['levelbg']));

			if (!(empty($member['sns_level']))) {
				$level = pdo_fetch('select * from ' . tablename('jy_sns_level') . ' where id=:id  limit 1', array(':id' => $member['sns_level']));
			}
			$row['member'] = array('id' => $member['id']);
			$row['level'] = $level;
			$row['floor'] = (($pindex - 1) * $psize) + $key + 2;
			if($row['mid'] == $post['mid']){
				$row['isAuthor'] = 1;
			}			
			$row['isManager'] = isManager($row['bid'], $row['mid']);
		}
		if($post['mid']==$_SESSION['uid']){
			$return['self']=1;
		}
		$return['pisManager']=$pisManager;
		$return['list']=$list;
		$return['post']=$post;
		$return['level']=$level;
		$return['board']=$board;
		$return['goodcount']=$goodcount;
		$return['replycount']=$replycount;
		$return['canpost']=$canpost;
		$return['member']=$member;
		$return['check']=$check;
		$return['catelist']=$catelist;
		$return['isgood']=$isgood;
		$return['isManager']=$isManager;
		$return['isSuperManager']=$isSuperManager;
		$return['sharedata']=array(
				'title'=>$post['title']?:'全网论坛',
				'desc'=>$basic['share_desc'],
				'link'=>custom_url('jy_sns/post.index//id='.$id,true),
				'imgUrl'=>ATTACHMENT_URL.$basic['share_icon'],
		);
		set_html('title',$post['title']);
		exi($return);
}
if($_RQ['op']=='complain'){
	$id = intval($_RQ['id']);
		if (empty($id)) {
			exi('参数错误','error','/post.index/main/id='.$id);
		}
		$catelist = pdo_getall('jy_sns_complaincate',array('uniacid'=>$_SESSION['uniacid']));

		$post = getPost($id);
		if (empty($post)) {
				exi('未找到话题','error','/post.index/main/id='.$id);	
		}
		$return['catelist']=$catelist;
		$return['item']['id']=	$id;
		exi($return);
}
if($_RQ['op']=='reply'){
	$id = intval($_RQ['id']);
	$rid = intval($_RQ['rid']);
		if (empty($id)) {
			exi('参数错误','error','/post.index/main/id='.$id);
		}


		$post = getPost($id);
		if (empty($post)) {
				exi('未找到话题','error','/post.index/main/id='.$id);	
		}
		$return['item']['id']=	$id;
		$return['item']['rid']=	$rid;
		$return['item']['bid']=$post['bid'];
		exi($return);
}
if($_RQ['op']=='post1'){
		$post=get_postdata();
	
		if(!$post['content']){
			exi('至少十个字符','error');
		}
		
		$id = $post['id'];
		
		
		$time = time();
		$data = array('uniacid' => $_SESSION['uniacid'],'mid'=>$_SESSION['uid'], 'type'=>$post['type'] ,'postsid' => $id, 'createtime' => $time,  'complaint_text' => trim($post['content']));
		pdo_insert('jy_sns_complain', $data);

		exi('投诉成功','','/post.index/main/id='.$id);
}
if($_RQ['op']=='post'){
		$post=get_postdata();
		if(!$post['content']){
			exi('评论至少十个字符','error');
		}
		
		$id = $post['id'];
		$rpid = $post['rid'];
		if (empty($id)) 
		{
			exi('参数错误','error');
		}
		$board = pdo_get('jy_sns_board',array('id'=>$id));
		$checked = 0;
		
		if ($ismanager) {
			$checked = (($board['needcheckreplymanager'] ? 0 : 1));
		}else {
			$checked = (($board['needcheckreply'] ? 0 : 1));
		}

		if ($issupermanager) {
			$checked = 1;
		}
		$time = time();
		$info=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('avatar','nickname'));
		$data = array('uniacid' => $_SESSION['uniacid'], 'bid' => $post['bid'], 'pid' => $id, 'rpid' => $rpid, 'mid' => $_SESSION['uid'], 'avatar' => $info['avatar'], 'nickname' => $info['nickname'], 'createtime' => $time, 'replytime' => $time, 'content' => trim($post['content']),'images' => $post['images'],'checked' => $checked);
		pdo_insert('jy_sns_post', $data);
		pdo_update('jy_sns_post', array('replytime' => $time), array('id' => $id, 'uniacid' => $_SESSION['uniacid']));
		$board = pdo_get('jy_sns_board',array('id'=>$post['bid']));
		if($board['replycredit']){
			pdo_update('core_members',array('credit1 +='=>$board['replycredit']),array('id'=>$_SESSION['uid']));
		}
		exi('回复成功','','/post.index/main/id='.$id);
}
if($_RQ['op']=='best'){
	$post=get_postdata();
	$id =$post['id'];
	$bid=pdo_getcolumn('jy_sns_post',array('id'=>$id),'bid');
	$board = pdo_get('jy_sns_board',array('id'=>$bid));
	$posts = getPost($id);
		if (empty($id)) 
		{
			exi('参数错误','error');
		}
		$l=pdo_getcolumn('jy_sns_post',array('id'=>$id),'isbest');
		if($l){
			pdo_update('jy_sns_post',array('isbest'=>0),array('id'=>$id));
			if($board['bestcredit']){
			pdo_update('core_members',array('credit1 -='=>$board['bestcredit']),array('id'=>$posts['mid']));
			}
			exi('取消全站精华成功','error');
		}else{
			pdo_update('jy_sns_post',array('isbest'=>1),array('id'=>$id));
			
			if($board['bestcredit']){
			pdo_update('core_members',array('credit1 +='=>$board['bestcredit']),array('id'=>$posts['mid']));
			}
			exi('设置全站精华成功','error');
		}
		
}
if($_RQ['op']=='boardbest'){
	$post=get_postdata();
	$id =$post['id'];
	$posts = getPost($id);
		if (empty($id)) 
		{
			exi('参数错误','error');
		}
	$bid=pdo_getcolumn('jy_sns_post',array('id'=>$id),'bid');
	$board = pdo_get('jy_sns_board',array('id'=>$bid));
		$l=pdo_getcolumn('jy_sns_post',array('id'=>$id),'isboardbest');
		if($l){
			if($board['bestboardcredit']){
			pdo_update('core_members',array('credit1 -='=>$board['bestboardcredit']),array('id'=>$posts['mid']));
			}
			pdo_update('jy_sns_post',array('isboardbest'=>0),array('id'=>$id));
			exi('取消精华成功','error');
		}else{
			if($board['bestboardcredit']){
			pdo_update('core_members',array('credit1 +='=>$board['bestboardcredit']),array('id'=>$posts['mid']));
			}
			pdo_update('jy_sns_post',array('isboardbest'=>1),array('id'=>$id));
			exi('设置精华成功','error');
		}
		
}
if($_RQ['op']=='top'){
	$post=get_postdata();
	$id =$post['id'];
	$posts = getPost($id);
		if (empty($id)) 
		{
			exi('参数错误','error','/index/');
		}
		$bid=pdo_getcolumn('jy_sns_post',array('id'=>$id),'bid');
		$board = pdo_get('jy_sns_board',array('id'=>$bid));
		$l=pdo_getcolumn('jy_sns_post',array('id'=>$id),'istop');
		if($l){
			if($board['topcredit']){
			pdo_update('core_members',array('credit1 -='=>$board['topcredit']),array('id'=>$posts['mid']));
			}
			pdo_update('jy_sns_post',array('istop'=>0),array('id'=>$id));
			exi('取消全站置顶成功','error');
		}else{
			if($board['topcredit']){
			pdo_update('core_members',array('credit1 +='=>$board['topcredit']),array('id'=>$posts['mid']));
			}
			pdo_update('jy_sns_post',array('istop'=>1),array('id'=>$id));
			exi('设置全站置顶成功','error');
		}
		
}
if($_RQ['op']=='boardtop'){
	$post=get_postdata();
	$id =$post['id'];
	$posts = getPost($id);
		if (empty($id)) 
		{
			exi('参数错误','error','/index/');
		}
		$bid=pdo_getcolumn('jy_sns_post',array('id'=>$id),'bid');
		$board = pdo_get('jy_sns_board',array('id'=>$bid));
		$l=pdo_getcolumn('jy_sns_post',array('id'=>$id),'isboardtop');
		if($l){
			if($board['topboardcredit']){
			pdo_update('core_members',array('credit1 -='=>$board['topboardcredit']),array('id'=>$posts['mid']));
			}
			pdo_update('jy_sns_post',array('isboardtop'=>0),array('id'=>$id));
			exi('取消置顶成功','error');
		}else{
			if($board['topboardcredit']){
			pdo_update('core_members',array('credit1 +='=>$board['topboardcredit']),array('id'=>$posts['mid']));
			}
			pdo_update('jy_sns_post',array('isboardtop'=>1),array('id'=>$id));
			exi('设置置顶成功','error');
		}
		
}

if($_RQ['op']=='delete'){
	$post=get_postdata();
	$id =$post['id'];
	$posts = getPost($id);
		if (empty($id)) 
		{
			exi('参数错误','error','/post.index/main/id='.$id);
		}	
			$bid=pdo_getcolumn('jy_sns_post',array('id'=>$id),'bid');
			$board = pdo_get('jy_sns_board',array('id'=>$bid));
			if($board['postcredit']){
			pdo_update('core_members',array('credit1 -='=>$board['postcredit']),array('id'=>$posts['mid']));
			}
			pdo_update('jy_sns_post',array('deleted'=>1,'deletedtime'=>time()),array('id'=>$id));
			exi('删除成功1','error','/board.index//id='.$bid);
		
}
if($_RQ['op']=='good'){
	$post=get_postdata();
	$id =$post['id'];
		if (empty($id)) 
		{
			exi('参数错误','error','/index/');
		}
		$l=pdo_get('jy_sns_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
		if($l){
			pdo_delete('jy_sns_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			exi('jian');
		}
		pdo_insert('jy_sns_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
		exi('jia');
}
if($_RQ['op']=='check'){
	$post=get_postdata();
	$id =$post['id'];
		if (empty($id)) 
		{
			exi('参数错误','error','/post.index/main/id='.$id);
		}
		
			pdo_update('jy_sns_post',array('checked'=>1,'checktime'=>time()),array('id'=>$id));
			exi('审核成功','error','/post.index/main/id='.$id);
		
}
?>